<?php
/**
 * 权限数据模型
 * PHP version 5
 * @package   Antonia
 * @author    Antonia <471699715@qq.com>
 * @copyright 2016 Antonia.
 */
namespace Admin\Model;
use Think\Model;
class AuthsModel extends Model {

    protected $tableName = 'sys_auths'; 
    
    /**
     * 设置主键
     *
     * @author Antonia
     */
    protected $pk        = 'auth_id';

    /**
     * 自动验证
     *
     * @author Antonia
     */
    protected $_validate = array(
     );

    /**
     * 自动完成
     *
     * @author Antonia
     */
    protected $_auto     = array(
            array('controller', 'remove_xss', 3, 'function'),
            array('title', 'remove_xss', 3, 'function'),
            array('action', 'remove_xss', 3, 'function'),
        );



    /**
     * 取得用于生成select的options的数组
     *
     * @return array
     * @author Antonia
     */
    public function getOptions () {
        $groups = $this->field('auth_id,title,sortNum')
                        ->where('parent_id=0')
                        ->order('sortNum desc')
                        ->select();
        return $groups;
    }

    /**
     * 取得权限列表
     *
     * @return array
     * @author Antonia
     */
    public function getAuths ($auths) {
        $list = $this->order('sortNum asc, auth_id desc')
            ->field('auth_id,parent_id,title,sortNum')
            ->where('parent_id=0')
            ->select();
        $newlist = array();
        if($list) {
            foreach($list as $l) {
                $chirds = array();
                
                $chirdren = $this->order('sortNum asc, auth_id desc')
                            ->field('auth_id,title,sortNum')
                            ->where('parent_id=' . $l['auth_id'])
                            ->select(); 
                
                if($chirdren) {
                    $canDo = true;
                    foreach ($chirdren as $chird) {
                        $is_check = isset($auths) && in_array($chird['auth_id'], $auths);
                        $chird["canDo"] = $is_check; 
                        $canDo &= $is_check;
                        $chirds[] = $chird;
                    }
                } else {
                    $canDo = false;
                }
                $l['canDo'] = $canDo;
                $l['chird'] = $chirds;
                $newlist[] = $l;
            }
        }
        return $newlist;
    }
    /**
     * 取得当前登录者的权限列表
     *
     * @return array
     * @author Antonia
     */
    public function getAdminAuths () {


                    
        if(!empty($_SESSION[C('USER_AUTH_KEY')])) {
            $admin_id = $_SESSION[C('USER_AUTH_KEY')];
            
            $Admingroupauths   = D("Admingroupauths");
            $AdminGroupAuths   = $Admingroupauths->getAdminGroupAuths();

            $auths = array_unique($AdminGroupAuths); 
            $authList = array();

            foreach($auths as $auth) {
                $authInfo = $this->field("auth_id,controller,action")->find($auth);
                $authList[$authInfo['controller']][] = $authInfo['action'];
            }
            return $authList;
        } else {
            return array();
        }

    }
}
?>